<template>
	<view>
		<fa-navbar title="修改密码"></fa-navbar>
		<view class="p-2">
			<view class="pt-1 fa-user-form">
				<u-form :model="formData" ref="uForm">
					<u-form-item label="" left-icon=" iconfont icon-mima" :border-bottom="false">
						<u-input v-model="formData.OldPwd" :custom-style="{'font-size': '32rpx'}" height="80" placeholder="输入原密码" placeholder-style="color:#999999" maxlength="12"/>
					</u-form-item>
					<u-form-item label="" left-icon=" iconfont icon-bianji" :border-bottom="false">
						<u-input v-model="formData.NewPwd" :custom-style="{'font-size': '32rpx'}" height="80" placeholder="请输入新密码" placeholder-style="color:#999999" maxlength="12" type="password"/>
					</u-form-item>
					<u-form-item label="" left-icon=" iconfont icon-bianji" :border-bottom="false">
						<u-input v-model="formData.repeatNewPwd" :custom-style="{'font-size': '32rpx'}" height="80" placeholder="请再次输入新密码" placeholder-style="color:#999999" maxlength="12" type="password"/>
					</u-form-item>
				</u-form>
			</view>
			<view class="font24" style="color: #999;" :class="{'col-error': errorTips != '6-12位数字或字母以及组合'}" v-if="errorTips">{{ errorTips }}</view>
			<view class="pt-4">
				<u-button 
					:type="!formValid ? 'default' : 'primary'"
					shape="circle"
					class="btn-grad" 
					:class="formValid ? '' : 'btn-disable'"
					:disabled="!formValid"
					@click="confirmPassword"
				>确定</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import faNavbar from '@/components/fa-navbar/fa-navbar.vue'
	import rules from '@/util/plugins/rules.js'
	export default {
		components: {
			faNavbar
		},
		data() {
			return {
				errorTips: '6-12位数字或字母以及组合',
				formData: {
					OldPwd: '',
					NewPwd: '',
					repeatNewPwd: ''
				},
			}
		},
		computed: {
			// 表单检验
			formValid() {
				return this.formData.OldPwd.length > 5 && this.formData.NewPwd.length > 5 && this.formData.repeatNewPwd.length > 5
			}
		},
		methods: {
			// 更改密码
			confirmPassword() {
				if (!rules['passw'].test(this.formData.NewPwd)) {
					return this.errorTips = '请设置6-12位数字或字母以及组合的密码'
				}
				if (this.formData.NewPwd != this.formData.repeatNewPwd) {
					return this.errorTips = '两次密码输入不一致，请重新输入'
				}
				if (this.formData.OldPwd == this.formData.NewPwd) {
					return this.errorTips = '新密码和旧密码一致'
				}
				this.errorTips = '6-12位数字或字母以及组合';
				this.$api.resetPassword(this.formData)
					.then(res => {
						if (res.code == 0) {
							uni.showToast({
								title: '修改成功，请重新登录',
								icon: 'none'
							})
							uni.removeStorageSync('TOKEN');
							uni.removeStorageSync('USER_ID');
							if (plus.os.name == 'Android') {
								this.$store.state.isSocketOpen = false;
								this.$keepAlive.stopKeepAlive();
							}
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/login/login'
								});
							}, 800);
						} else {
							uni.showToast({title: '修改失败，原密码错误', icon: 'none'});
						}
					})
					.catch(err => console.error(err));
			}
		}
	}
</script>

<style lang="scss" scoped>
	.col-error {
		color: #E55D5D !important;
	}
</style>
